﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MockPrjTeam1.Biz;
using MockPrjTeam1.Entity;
using System.Windows.Forms;

namespace MockPrjTeam1.Pages.Forms
{
    public partial class BusinessDetail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var id = Convert.ToString(Request.QueryString["id"]);
            if (!string.IsNullOrEmpty(id))
            {
                if (!IsPostBack)
                {
                    var biz = new SearchBiz();
                    var result = new Search();
                    result = biz.GetOneBusiness(Convert.ToInt32(id));
                    txtID.Text = id;
                    txtBusinessName.Text = result.businessName;
                    txtsicCode.Text = result.sicCode;                   
                }
            }

        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            var Entity = new Search();
            var biz = new SearchBiz();
            Entity.businessName = Convert.ToString(txtBusinessName.Text);
            Entity.sicCode = Convert.ToString(txtsicCode.Text);
            
            if (!String.IsNullOrEmpty(Convert.ToString(txtID.Text)))
            {
                Entity.businessID = Convert.ToInt32(txtID.Text);
                var result = biz.EditBusiness(Entity);
                if (result)
                {
                    MessageBox.Show("Success");
                    Response.Redirect("/Pages/Views/SearchBusiness.aspx");
                }
                else
                {
                    MessageBox.Show("Fail");
                }
            }
            else
            {
                var temp = biz.GetIdBusiness();
                Entity.businessID = temp;
                var result = biz.CreateBusiness(Entity);
                if (result)
                {
                    MessageBox.Show("Success");
                    Response.Redirect("/Pages/Views/SearchBusiness.aspx");
                }
                else
                {
                    MessageBox.Show("Fail");
                }

            }




        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("/Pages/Views/SearchBusiness.aspx");
        }

        protected void CustomValidator_SicCode(object source, ServerValidateEventArgs args)
        {
            var ID = txtID.Text;
            if (String.IsNullOrEmpty(ID))
            {
                ID = "0";
            }
            var biz = new SearchBiz();
            var Entity = new Search();
            Entity.businessID = Convert.ToInt32(ID);
            Entity.sicCode = Convert.ToString(txtsicCode.Text);
            if (biz.ValidSicCode(Entity.businessID, Entity.sicCode))
            {
                args.IsValid = true;
            }
            else
                args.IsValid = false;
        }
    }
}